package com.practicesuite.controllayer;

import java.awt.Dimension;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.practicesuite.systemlayer.Doctor;
import com.practicesuite.systemlayer.Invoice;
import com.practicesuite.systemlayer.Nurse;
import com.practicesuite.systemlayer.Patient;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.TreatmentRecord;
import com.practicesuite.systemlayer.User;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.JScrollPane;

/**
 * Panel for filling out information to create a new treatment record.
 * 
 * @author James
 *
 */
public class CreateTreatmentPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    private GregorianCalendar calendar;
    private int yearNow;
    private String[] years;
    private List<Patient> patients;
    String[] patientNames;
    private List<Doctor> doctors;
    String[] doctorNames;
    private List<Nurse> nurses;
    String[] nurseNames;
    
    private JTextField textFieldChiefComplaint;
    private JTextField textFieldBloodPressure;
    private JTextField textFieldPulse;
    private JTextField textFieldTempurature;
    private JTextField textFieldHeight;
    private JTextField textFieldWeight;
    private JTextField textFieldDiagnosis;
    private JTextField textFieldServices1;
    private JTextField textFieldServicesPrice1;
    private JTextField textFieldServices2;
    private JTextField textFieldServicesPrice2;
    private JTextField textFieldServices3;
    private JTextField textFieldServicesPrice3;
    private JTextField textFieldDiscount;
    private JTextField textFieldDiscountAmount;

    /**
     * Create the panel.
     * @throws Exception 
     */
    public CreateTreatmentPanel(MainWindow container, final User user) throws Exception {
        this.window = container;
        setPreferredSize(new Dimension(500, 400));

        patients = SystemDriver.getPatientList();
        patientNames = new String[patients.size() + 1];
        patientNames[0] = "Select";
        for (int i = 1; i <= patients.size(); i++) {
            patientNames[i] = patients.get(i - 1).getName();
        }
        
        doctors = SystemDriver.getDoctorList();
        doctorNames = new String[doctors.size() + 1];
        doctorNames[0] = "Select";
        for (int i = 1; i <= doctors.size(); i++) {
            doctorNames[i] = doctors.get(i - 1).getName();
        }

        nurses = SystemDriver.getNurseList();
        nurseNames = new String[nurses.size() + 1];
        nurseNames[0] = "Select";
        for (int i = 1; i <= nurses.size(); i++) {
            nurseNames[i] = nurses.get(i - 1).getName();
        }
        
        calendar = new GregorianCalendar();
        yearNow = calendar.get(GregorianCalendar.YEAR);
        years = new String[] {"Year", Integer.toString(yearNow - 1), Integer.toString(yearNow), 
                Integer.toString(yearNow + 1), Integer.toString(yearNow + 2), Integer.toString(yearNow + 3)};
        setLayout(new GridLayout(0, 1, 0, 0));
        
        JPanel panel_7 = new JPanel();
        panel_7.setLayout(new GridLayout(0, 1, 0, 0));
        panel_7.setPreferredSize(new Dimension(500, 500));
        
        JLabel lblRequestAppointment = new JLabel("Create New Treatment Record");
        panel_7.add(lblRequestAppointment);
        lblRequestAppointment.setHorizontalAlignment(SwingConstants.CENTER);
        lblRequestAppointment.setFont(new Font("Tahoma", Font.PLAIN, 24));
        
        JPanel panel_9 = new JPanel();
        FlowLayout flowLayout_6 = (FlowLayout) panel_9.getLayout();
        flowLayout_6.setAlignment(FlowLayout.LEADING);
        panel_7.add(panel_9);
        
        JLabel lblPatient = new JLabel(" Patient: ");
        lblPatient.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_9.add(lblPatient);
        
        final JComboBox comboBoxPatient = new JComboBox();
        comboBoxPatient.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxPatient.setModel(new DefaultComboBoxModel(patientNames));
        panel_9.add(comboBoxPatient);
        
        JLabel lblReasonForAppointment = new JLabel("     Treating Doctor:");
        panel_9.add(lblReasonForAppointment);
        lblReasonForAppointment.setHorizontalAlignment(SwingConstants.CENTER);
        lblReasonForAppointment.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        final JComboBox comboBoxDoctor = new JComboBox();
        panel_9.add(comboBoxDoctor);
        comboBoxDoctor.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxDoctor.setModel(new DefaultComboBoxModel(doctorNames));
        
        JLabel lblAdditionalNotesoptional = new JLabel("      Attending Nurse:");
        panel_9.add(lblAdditionalNotesoptional);
        lblAdditionalNotesoptional.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        final JComboBox comboBoxNurse = new JComboBox();
        panel_9.add(comboBoxNurse);
        comboBoxNurse.setModel(new DefaultComboBoxModel(nurseNames));
        comboBoxNurse.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        JPanel panel = new JPanel();
        panel_7.add(panel);
        FlowLayout flowLayout_4 = (FlowLayout) panel.getLayout();
        flowLayout_4.setAlignment(FlowLayout.LEFT);
        
        JLabel lblDate = new JLabel(" Date:");
        lblDate.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(lblDate);
        
        final JComboBox comboBoxMonth = new JComboBox();
        comboBoxMonth.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxMonth.setModel(new DefaultComboBoxModel(new String[] {"Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
        comboBoxMonth.setSelectedIndex(0);
        panel.add(comboBoxMonth);
        
        final JComboBox comboBoxDay = new JComboBox();
        comboBoxDay.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxDay.setModel(new DefaultComboBoxModel(new String[] {"Day", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
        panel.add(comboBoxDay);
        
        final JComboBox comboBoxYear = new JComboBox();
        comboBoxYear.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxYear.setModel(new DefaultComboBoxModel(years));
        panel.add(comboBoxYear);
        
        JLabel lblTime = new JLabel("     Time:");
        lblTime.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(lblTime);
        
        final JComboBox comboBoxTime = new JComboBox();
        comboBoxTime.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxTime.setModel(new DefaultComboBoxModel(new String[] {"Select", "8:00 am", "8:30 am", "9:00 am", "9:30 am", "10:00 am", "10:30 am", "11:00 am", "11:30 am", "12:00 pm", "12:30 pm", "1:00 pm", "1:30 pm", "2:00 pm", "2:30 pm", "3:00 pm", "3:30 pm", "4:00 pm", "4:30 pm", "5:00 pm", "5:30 pm", "6:00 pm", "6:30 pm"}));
        panel.add(comboBoxTime);
        
        JPanel panel_10 = new JPanel();
        panel_7.add(panel_10);
        panel_10.setLayout(new GridLayout(0, 1, 0, 0));
        
        JPanel panel_1 = new JPanel();
        panel_10.add(panel_1);
        FlowLayout flowLayout_3 = (FlowLayout) panel_1.getLayout();
        flowLayout_3.setVgap(2);
        flowLayout_3.setAlignment(FlowLayout.LEFT);
        
        JLabel lblServicesProvided = new JLabel(" Services Provided:   ");
        lblServicesProvided.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_1.add(lblServicesProvided);
        
        textFieldServices1 = new JTextField();
        textFieldServices1.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_1.add(textFieldServices1);
        textFieldServices1.setColumns(14);
        
        JLabel lblPrice = new JLabel("     Price: $");
        lblPrice.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_1.add(lblPrice);
        
        textFieldServicesPrice1 = new JTextField();
        textFieldServicesPrice1.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_1.add(textFieldServicesPrice1);
        textFieldServicesPrice1.setColumns(10);
        
        JPanel panel_11 = new JPanel();
        FlowLayout flowLayout_7 = (FlowLayout) panel_11.getLayout();
        flowLayout_7.setVgap(2);
        flowLayout_7.setAlignment(FlowLayout.LEFT);
        panel_10.add(panel_11);
        
        JLabel label = new JLabel("                                       ");
        panel_11.add(label);
        
        textFieldServices2 = new JTextField();
        textFieldServices2.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_11.add(textFieldServices2);
        textFieldServices2.setColumns(14);
        
        JLabel label_1 = new JLabel("     Price: $");
        label_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_11.add(label_1);
        
        textFieldServicesPrice2 = new JTextField();
        textFieldServicesPrice2.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldServicesPrice2.setColumns(10);
        panel_11.add(textFieldServicesPrice2);
        
        JPanel panel_2 = new JPanel();
        panel_7.add(panel_2);
        panel_2.setLayout(new GridLayout(0, 1, 0, 0));
        
        JPanel panel_12 = new JPanel();
        FlowLayout flowLayout_2 = (FlowLayout) panel_12.getLayout();
        flowLayout_2.setVgap(2);
        flowLayout_2.setAlignment(FlowLayout.LEFT);
        panel_2.add(panel_12);
        
        JLabel label_3 = new JLabel("                                       ");
        panel_12.add(label_3);
        
        textFieldServices3 = new JTextField();
        textFieldServices3.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldServices3.setColumns(14);
        panel_12.add(textFieldServices3);
        
        JLabel label_4 = new JLabel("     Price: $");
        label_4.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_12.add(label_4);
        
        textFieldServicesPrice3 = new JTextField();
        textFieldServicesPrice3.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldServicesPrice3.setColumns(10);
        panel_12.add(textFieldServicesPrice3);
        
        JPanel panel_13 = new JPanel();
        FlowLayout flowLayout_8 = (FlowLayout) panel_13.getLayout();
        flowLayout_8.setVgap(2);
        flowLayout_8.setAlignment(FlowLayout.LEFT);
        panel_2.add(panel_13);
        
        JLabel lblApplicableDiscounts = new JLabel(" Applicable Discount: ");
        lblApplicableDiscounts.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_13.add(lblApplicableDiscounts);
        
        textFieldDiscount = new JTextField();
        textFieldDiscount.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_13.add(textFieldDiscount);
        textFieldDiscount.setColumns(14);
        
        JLabel lblAmount = new JLabel(" Amount: $");
        lblAmount.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_13.add(lblAmount);
        
        textFieldDiscountAmount = new JTextField();
        textFieldDiscountAmount.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldDiscountAmount.setColumns(10);
        panel_13.add(textFieldDiscountAmount);
        
        JPanel panel_3 = new JPanel();
        panel_7.add(panel_3);
        FlowLayout flowLayout_1 = (FlowLayout) panel_3.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        
        JLabel lblDesiredDoctor = new JLabel(" Chief Complaint:");
        lblDesiredDoctor.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_3.add(lblDesiredDoctor);
        
        textFieldChiefComplaint = new JTextField();
        textFieldChiefComplaint.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_3.add(textFieldChiefComplaint);
        textFieldChiefComplaint.setColumns(15);
        
        JPanel panel_5 = new JPanel();
        panel_7.add(panel_5);
        panel_5.setPreferredSize(new Dimension(500, 25));
        panel_5.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        
        JLabel lblVitalSigns = new JLabel(" Vital Signs:");
        lblVitalSigns.setFont(new Font("Tahoma", Font.PLAIN, 16));
        panel_5.add(lblVitalSigns);
        
        JLabel lblBloodPressure = new JLabel(" Blood Pressure:");
        panel_5.add(lblBloodPressure);
        lblBloodPressure.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldBloodPressure = new JTextField();
        panel_5.add(textFieldBloodPressure);
        textFieldBloodPressure.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldBloodPressure.setColumns(5);
        
        JLabel lblPulse = new JLabel(" Pulse:");
        panel_5.add(lblPulse);
        lblPulse.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldPulse = new JTextField();
        panel_5.add(textFieldPulse);
        textFieldPulse.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldPulse.setColumns(5);
        
        JPanel panel_6 = new JPanel();
        panel_7.add(panel_6);
        FlowLayout flowLayout = (FlowLayout) panel_6.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        panel_6.setPreferredSize(new Dimension(500, 75));
        
        JLabel lblTempurature = new JLabel("           Tempurature:");
        panel_6.add(lblTempurature);
        lblTempurature.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldTempurature = new JTextField();
        panel_6.add(textFieldTempurature);
        textFieldTempurature.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldTempurature.setColumns(5);
        
        JLabel lblHeight = new JLabel(" Height:");
        panel_6.add(lblHeight);
        lblHeight.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldHeight = new JTextField();
        panel_6.add(textFieldHeight);
        textFieldHeight.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldHeight.setColumns(5);
        
        JLabel lblWeight = new JLabel(" Weight:");
        panel_6.add(lblWeight);
        lblWeight.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldWeight = new JTextField();
        panel_6.add(textFieldWeight);
        textFieldWeight.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textFieldWeight.setColumns(5);
        
        JPanel panel_8 = new JPanel();
        FlowLayout flowLayout_5 = (FlowLayout) panel_8.getLayout();
        flowLayout_5.setAlignment(FlowLayout.LEFT);
        panel_7.add(panel_8);
        
        JLabel lblDiagnosis = new JLabel(" Diagnosis: ");
        lblDiagnosis.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_8.add(lblDiagnosis);
        
        textFieldDiagnosis = new JTextField();
        textFieldDiagnosis.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_8.add(textFieldDiagnosis);
        textFieldDiagnosis.setColumns(15);
        
        JPanel panel_4 = new JPanel();
        panel_7.add(panel_4);
        
        JButton btnRequestAppointment = new JButton("Create Treatment Record");
        btnRequestAppointment.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String month = (String)comboBoxMonth.getSelectedItem();
                String day = (String)comboBoxDay.getSelectedItem();
                String year = (String)comboBoxYear.getSelectedItem();
                String time = (String)comboBoxTime.getSelectedItem();
                String patientName = (String)comboBoxPatient.getSelectedItem();
                int patientIndex = comboBoxPatient.getSelectedIndex() - 1;
                String doctorName = (String)comboBoxDoctor.getSelectedItem();
                String nurseName = (String)comboBoxNurse.getSelectedItem();
                
                if (month.equals("Month")) {
                    JOptionPane.showMessageDialog(null, "Please select a month.");
                }
                else if (day.equals("Day")) {
                    JOptionPane.showMessageDialog(null, "Please select a day.");
                }
                else if (year.equals("Year")) {
                    JOptionPane.showMessageDialog(null, "Please select a year.");
                }
                else if (time.equals("Select")) {
                    JOptionPane.showMessageDialog(null, "Please select a time.");
                }
                else if (patientName.equals("Select")) {
                    JOptionPane.showMessageDialog(null, "Please select the treated patient.");
                }
                else if (doctorName.equals("Select")) {
                    JOptionPane.showMessageDialog(null, "Please select the treating doctor.");
                }
                else if (nurseName.equals("Select")) {
                    JOptionPane.showMessageDialog(null, "Please select the attending nurse.");
                }
                else {
                    GregorianCalendar calendar = new GregorianCalendar(Integer.parseInt(year),
                            comboBoxMonth.getSelectedIndex() - 1, Integer.parseInt(day));
                    Date date = calendar.getTime();
                    Patient treatedPatient = patients.get(patientIndex);
                    Doctor treatingDoctor = doctors.get(comboBoxDoctor.getSelectedIndex() - 1);
                    Nurse attendingNurse = nurses.get(comboBoxNurse.getSelectedIndex() - 1);
                    String chiefComplaint = textFieldChiefComplaint.getText();
                    int[] vitalSigns = {Integer.parseInt(textFieldBloodPressure.getText()),
                                Integer.parseInt(textFieldPulse.getText()),
                                Integer.parseInt(textFieldTempurature.getText()),
                                Integer.parseInt(textFieldHeight.getText()),
                                Integer.parseInt(textFieldWeight.getText())};
                    String diagnosis = textFieldDiagnosis.getText();
                    
                    String service1 = textFieldServices1.getText();
                    String service2 = textFieldServices2.getText();
                    String service3 = textFieldServices3.getText();
                    String discount = textFieldDiscount.getText();
                    
                    double servicePrice1 = 1;
                    double servicePrice2 = 2;
                    double servicePrice3 = 5;
                    double discountAmount = 7;
                    
                    try {
                        servicePrice1 = Double.parseDouble(textFieldServicesPrice1.getText());
                        servicePrice2 = Double.parseDouble(textFieldServicesPrice2.getText());
                        servicePrice3 = Double.parseDouble(textFieldServicesPrice3.getText());
                        discountAmount = Double.parseDouble(textFieldDiscountAmount.getText());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    
                    TreatmentRecord t = new TreatmentRecord(treatingDoctor.getUserID(), date, attendingNurse.getUserID(), 
                            chiefComplaint, vitalSigns, diagnosis, 0);
                    int tID = SystemDriver.createTreatmentRecord(t, treatedPatient.getUserID());
                    t.setTreatRecId(tID);
                    
                    Map<Double, String> services = new HashMap<Double, String>();
                    if (service1.length() > 0) {
                        services.put(servicePrice1, service1);
                    }
                    if (service2.length() > 0) {
                        services.put(servicePrice2, service2);
                    }
                    if (service3.length() > 0) {
                        services.put(servicePrice3, service3);
                    }
                    Map<Double, String> discounts = new HashMap<Double, String>();
                    if (discount.length() > 0) {
                        discounts.put(discountAmount, discount);
                    }
                    
                    Invoice i = new Invoice(treatedPatient, date, services, discounts, 0, treatingDoctor.getName(), tID);
                    SystemDriver.setInvoice(i, t, doctorName);
                    
                    JOptionPane.showMessageDialog(null, "Treatment record created successfully.");
                    int eUID = user.getEUID();
                    if (eUID == 0) {
                        window.gotoPanel("adminHome", user);
                    }
                    else if (eUID == 1) {
                        window.gotoPanel("doctorHome", user);
                    }
                    else if (eUID == 2) {
                        window.gotoPanel("nurseHome", user);
                    }
                }
            }
        });
        panel_4.add(btnRequestAppointment);
        btnRequestAppointment.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JButton btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int eUID = user.getEUID();
                if (eUID == 0) {
                    window.gotoPanel("adminHome", user);
                }
                else if (eUID == 1) {
                    window.gotoPanel("doctorHome", user);
                }
                else if (eUID == 2) {
                    window.gotoPanel("nurseHome", user);
                }
            }
        });
        btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_4.add(btnCancel);
        
        JScrollPane scrollPane = new JScrollPane(panel_7);
        add(scrollPane);

    }

}
